15. 碰撞的概率 [练习区]

碰撞的概率 [练习区]

车辆1\车辆2 左转 直行 右转
左转 0.5 0.25 0.1
直行 0.25 0.02 0.1
右转 0.1 0.1 0.01

上面显示的真值表列出了两辆车同时尝试移动,则两辆车在十字路口发生碰撞的概率。例如,如果车辆 1 直行,车辆 2 左转,则碰撞概率为 0.25。

Start Quiz:

def probability_of_collision(car_1, car_2):
    """
    Calculate the probablity of a collision based on the car turns
    Args:
        car_1 (string): The turning direction of car_1
        car_2 (string): The turning direction of car_2
        
    Returns:
        float: the probability of a collision
    """
    # car_1 and car_2 will each be strings whose value will either be 
    # "L" for left, "S" for straight, or "R" for right.
    probability = 0.0 # you should change this value based on the directions.
    
    if car_1 == "L":
        # TODO your code here for when car 1 turns left
    elif car_1 == "S":
        # TODO your code here for when car 1 goes straight
    else:
        # TODO your code here for when car 1 turns right
    
    return probability


# This function is used to test the correctness of your code. You shouldn't
# touch any of the code below here (but feel free to look through it to
# understand what "correct" looks like).
def test():
    num_correct = 0
    
    p1 = probability_of_collision("L", "L")
    if p1 == 0.5:
        num_correct += 1
    
    p2 = probability_of_collision("L", "R")
    if p2 == 0.1:
        num_correct += 1
    
    p3 = probability_of_collision("L", "S")
    if p3 == 0.25:
        num_correct += 1
    
    p4 = probability_of_collision("S", "R")
    if p4 == 0.1:
        num_correct += 1
    
    print("You got", num_correct, "out of 4 correct")
    
test()